package com.tgy.JUC.Pool;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/***
 * @ClassName: Demo1
 * @Description: 测试Executors 相当于工具类
 * @Auther: 送你一块二向箔
 * @Date: 10:37 2022/3/14
 * @version : V1.0
 */
public class Demo1 {
    public static void main(String[] args) {
        //run1();
        run3();
    }

    public static void run1(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
            for (int i = 0; i < 10; i++) {
                //使用了线程池之后要用线程池的方法创建线程
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        } finally {
            executorService.shutdown();
        }
    }
    public static void run2(){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        try {
            for (int i = 0; i < 10; i++) {
                //使用了线程池之后要用线程池的方法创建线程
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    public static void run3(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            for (int i = 0; i < 100; i++) {
                //使用了线程池之后要用线程池的方法创建线程
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName());
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }


    public void test(){
        Executors.newFixedThreadPool(5);//固定线程
        Executors.newSingleThreadExecutor();//单个线程
        Executors.newCachedThreadPool();//适应性变化
        Executors.newScheduledThreadPool(3);
    }
}
